package com.smscrypt;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SmsSendActivity extends Activity {
	private Button btnSend = (Button) findViewById(R.id.btnSend);
	private TextView edtText = (TextView) findViewById(R.id.edtMessage);
	private AutoCompleteTextView edtPhone = (AutoCompleteTextView) findViewById(R.id.autoPhone);
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_send);
        
        //Try to make a autocomplete from a DB-dump with: PhoneNr <Firstname Lastname>
        try{
            String[] test = new String[] {"+491234 <Deine Mutta>","+496666 <Der Teufel>"};
            ArrayAdapter<String> adapter = new ArrayAdapter<String>
            (this,android.R.layout.simple_dropdown_item_1line,test);
            edtPhone.setAdapter(adapter);
        } catch (Exception e){
        	Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        }
     
        edtText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				//If defautlt message is in the field -> delete it
				if(hasFocus && edtText.getText().equals(R.string.enterMessage)) 
					edtText.setText("");
				else if(edtText.getText().equals(""))
					//If no text were inputed restore the default message
						edtText.setText(R.string.enterMessage);				
			}
		});
        
        edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(hasFocus){
					//If defautlt message is in the field -> delete it
					if(edtPhone.getText().equals(R.string.enterPhoneNumber)) 
						edtPhone.setText("");
				} else {
					//If no text were inputed restore the default message
					if(edtPhone.getText().equals("")) 
						edtPhone.setText(R.string.enterPhoneNumber);
				}
			}
		});
        
        btnSend.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String nr = edtPhone.getText().toString().split(" ")[0];
				String text = edtText.getText().toString();
				
				//TODO: Encrypt text here
				Toast.makeText(SmsSendActivity.this, "Encryption not supported yet", Toast.LENGTH_LONG).show();
				
				sendSMS(nr,text);
				//back to last activity
				finish();
			}
		});

	}
	
	  
	private void sendSMS(String phoneNumber, String message)
	    {        
	        String SENT = "SMS_SENT";
	        String DELIVERED = "SMS_DELIVERED";
	 
	        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
	            new Intent(SENT), 0);
	 
	        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
	            new Intent(DELIVERED), 0);
	 
	        //---when the SMS has been sent---
	        registerReceiver(new BroadcastReceiver(){
	            @Override
	            public void onReceive(Context arg0, Intent arg1) {
	                switch (getResultCode())
	                {
	                    case Activity.RESULT_OK:
	                        Toast.makeText(getBaseContext(), R.string.smsSentString, 
	                                Toast.LENGTH_SHORT).show();
	                        break;
	                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
	                        Toast.makeText(getBaseContext(), R.string.genericFailureString, 
	                                Toast.LENGTH_SHORT).show();
	                        break;
	                    case SmsManager.RESULT_ERROR_NO_SERVICE:
	                        Toast.makeText(getBaseContext(), R.string.noServiceString, 
	                                Toast.LENGTH_SHORT).show();
	                        break;
	                    case SmsManager.RESULT_ERROR_NULL_PDU:
	                        Toast.makeText(getBaseContext(), R.string.nullPduString, 
	                                Toast.LENGTH_SHORT).show();
	                        break;
	                    case SmsManager.RESULT_ERROR_RADIO_OFF:
	                        Toast.makeText(getBaseContext(), R.string.radioOffString, 
	                                Toast.LENGTH_SHORT).show();
	                        break;
	                }
	            }
	        }, new IntentFilter(SENT));
	 
	        //---when the SMS has been delivered---
	        registerReceiver(new BroadcastReceiver(){
	            @Override
	            public void onReceive(Context arg0, Intent arg1) {
	                switch (getResultCode())
	                {
	                    case Activity.RESULT_OK:
	                        Toast.makeText(getBaseContext(), R.string.smsDeliveredString, 
	                                Toast.LENGTH_SHORT).show();
	                        break;
	                    case Activity.RESULT_CANCELED:
	                        Toast.makeText(getBaseContext(), R.string.smsNotDeliveredString, 
	                                Toast.LENGTH_SHORT).show();
	                        break;                        
	                }
	            }
	        }, new IntentFilter(DELIVERED));        
	 
	        SmsManager sms = SmsManager.getDefault();
	        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
	    }
	 

}
